import React, { FunctionComponent } from "react";
import { HashRouter, Route, Redirect, Switch } from "react-router-dom";
import { useClientLanguage } from "@anyshare/common/lib/hooks";
import { languages } from "@anyshare/common/lib/constants";
import {
    IntlProvider,
    LanguageProvider,
    AuthProvider,
    AppManagerProvider,
    AppActivator,
    ConfigProvider,
    AntdConfigProvider,
    FileHandlerProvider,
    IconThemeProvider,
    ToastProvider,
} from "@anyshare/components";
import { AppContainerManagerProvider } from "@anyshare/components/lib/AppContainerManagerProvider/ElectronAppContainerManagerProvider";
import { EventBusProvider } from "@anyshare/components/lib/EventBusProvider/ElectronEventBusProvider";
import { LogProvider } from "@anyshare/components/lib/LogProvider";
import { ContextMenuProvider } from "@anyshare/controls";
import AppPage from "./pages/_app";
import openapi from "@anyshare/openapi";
import {
    copy,
    dir,
    messagebox,
    move,
    select,
    settings,
    download,
    size,
    newdir,
    rename,
    search,
    foxitreader,
    remove,
    metadata,
    tags,
    summary,
    applist,
    sharereview,
    shareapply,
    flowreview,
    flowapply,
    securityreview,
    securityapply,
    recycle,
    quaratine,
    message,
    filelock,
    contact,
    device,
    securitylevel,
    share,
    sendmail,
    approvaltips,
    shareapplytips,
    accessoradded,
    fileflow,
    previewimg,
    play,
    privacyinfo,
    officeonline,
    cadpreview,
    dragmove,
    location,
    webfoxitreader,
    crossdomain,
    crosssync,
    crosslist,
    crossgoing,
    wpspreview,
    opendir,
    microWidgets,
    microWidgetsDialog,
    microWidgetsForTaskBar,
    knowledgeDoc,
    microWidgetsFullScreen,
} from "@anyshare/apps";
import smartsheet from "@anyshare/apps/lib/smartsheet";
import kc from "@anyshare/apps/lib/kc";
import workflow from "@anyshare/apps/lib/workflow";
import upgrade from "@anyshare/apps/lib/upgrade";
import newdownloadtask from "@anyshare/apps/lib/newdownloadtask";
import logoutexit from "@anyshare/apps/lib/logoutexit";
import syncdetails from "@anyshare/apps/lib/syncdetails";
import signin from "@anyshare/apps/lib/signin";
import linkdetect from "@anyshare/apps/lib/linkdetect";
import aboutversion from "@anyshare/apps/lib/aboutversion";
import MainPage from "./pages/_main";
import BlankPage from "./pages/_blank";
import { MicroWidgetProvider, FrameSdkProvider } from "@anyshare/microwidget";

Object.assign(window, { openapi });

declare const __ANYSHARE_EXTENSIONS__: any;

const apps = [
    ...(__ANYSHARE_EXTENSIONS__ || []),
    copy,
    dir,
    messagebox,
    move,
    select,
    settings,
    download,
    signin,
    size,
    newdir,
    rename,
    search,
    foxitreader,
    remove,
    metadata,
    tags,
    upgrade,
    summary,
    applist,
    sharereview,
    shareapply,
    flowreview,
    flowapply,
    securityreview,
    securityapply,
    recycle,
    quaratine,
    message,
    filelock,
    contact,
    device,
    smartsheet,
    syncdetails,
    linkdetect,
    aboutversion,
    securitylevel,
    share,
    sendmail,
    approvaltips,
    shareapplytips,
    accessoradded,
    fileflow,
    previewimg,
    play,
    privacyinfo,
    newdownloadtask,
    logoutexit,
    officeonline,
    cadpreview,
    dragmove,
    location,
    webfoxitreader,
    crossdomain,
    crosssync,
    crosslist,
    crossgoing,
    wpspreview,
    kc,
    workflow,
    opendir,
    microWidgets,
    microWidgetsDialog,
    microWidgetsForTaskBar,
    knowledgeDoc,
    microWidgetsFullScreen,
];

const App: FunctionComponent = () => {
    const clientLanguage = useClientLanguage();

    const clientlang =
        clientLanguage === "zh-hk" || clientLanguage === "zh-tw"
            ? "zh-tw"
            : clientLanguage.startsWith("zh")
            ? "zh-cn"
            : "en-us";

    return (
        <HashRouter basename="/anyshare">
            <Switch>
                <Route
                    path={`/(${languages.join("|")})`}
                    render={({ match }) => (
                        <EventBusProvider>
                            <LanguageProvider>
                                <AppManagerProvider apps={apps}>
                                    <AntdConfigProvider>
                                        <IntlProvider>
                                            <AppActivator />
                                            <IconThemeProvider>
                                                <AppContainerManagerProvider>
                                                    <FileHandlerProvider>
                                                        <LogProvider>
                                                            <AuthProvider>
                                                                <ToastProvider>
                                                                    <ContextMenuProvider>
                                                                        <ConfigProvider>
                                                                            <MicroWidgetProvider>
                                                                                <FrameSdkProvider>
                                                                                    <Switch>
                                                                                        <Route
                                                                                            path={`${match.path}/_main`}
                                                                                            component={MainPage}
                                                                                        />
                                                                                        <Route
                                                                                            path={`${match.path}/_blank`}
                                                                                            component={BlankPage}
                                                                                        />
                                                                                        <Route
                                                                                            path={`${match.path}/:app/:slash*`}
                                                                                            component={AppPage}
                                                                                        />
                                                                                    </Switch>
                                                                                </FrameSdkProvider>
                                                                            </MicroWidgetProvider>
                                                                        </ConfigProvider>
                                                                    </ContextMenuProvider>
                                                                </ToastProvider>
                                                            </AuthProvider>
                                                        </LogProvider>
                                                    </FileHandlerProvider>
                                                </AppContainerManagerProvider>
                                            </IconThemeProvider>
                                        </IntlProvider>
                                    </AntdConfigProvider>
                                </AppManagerProvider>
                            </LanguageProvider>
                        </EventBusProvider>
                    )}
                ></Route>
                <Route
                    render={({ location }) => (
                        <Redirect
                            from="*"
                            to={{ pathname: `/${clientlang}${location.pathname}`, search: location.search }}
                        />
                    )}
                />
            </Switch>
        </HashRouter>
    );
};

export default App;
